import { useReducer } from "react";
const getDefaultCity =()=>{
    let defaultCity="武汉";
    if(localStorage.getItem("city")){
        defaultCity = localStorage.getItem("city")
    }
    return defaultCity
}
const useCityReducer = ()=>{
    const initState = {
        city:getDefaultCity()
    }
    //state 不要直接修改 ，需要做深拷贝
    const reducer = (state,action)=>{
        console.log(state,action);
        if(action.type==="changeCity"){
            localStorage.setItem("city",action.val)
            return{
                city:action.val
            }
        }
        return initState
    }
    const [state,dispatch] = useReducer(reducer,initState)
    return {state,dispatch}
}
export default useCityReducer